package cn.wxz1997.permission.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Description: ${todo}
 * @Author: wxz1997
 * @Date 18-8-4下午9:33
 */
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyFailureHandler myFailureHandler;

    @Autowired
    private MySuccessHandler mySuccessHandler;

    /**
     * 密码加密解密
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/login_request")  //需要认证时，登录界面所在的地址
                .loginProcessingUrl("/login") //访问该请求时,由UsernamePasswordAuthenticationFilter来处理,默认是login请求.
                .failureHandler(myFailureHandler)
                .successHandler(mySuccessHandler)
                .and()
                .authorizeRequests()
                .antMatchers("/login_request").permitAll() //防止无线循环重定向
                .antMatchers("/page/*").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .logout()
                .logoutUrl("/logout")
                .permitAll()
                .and()
                .csrf().disable();
    }
}
